package Main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class TheaterClient extends Thread
{
	private class Request
	{
		//public Booking Booking;
		public String FirstName, LastName;
		public String EventName;
		public String Date;
		public String Time;
		public int HallId;
		public ArrayList<Integer> PlaceNumbers;
		public Request(String first, String last, String event, String date, String time, int hallId)
		{
			
			//Booking=booking;
			FirstName=new String(first);
			LastName=new String(last);
			EventName=new String(event);
			Date=new String(date);
			Time=new String(time);
			HallId=hallId;
			PlaceNumbers=new ArrayList<Integer>();
		}
		public String toString()
		{
			String str="";
			str+=FirstName+" "+LastName+" "+" \""+EventName+"\" "+Date+" "+Time+" "+HallId;
			return str;
		}
	}
	private ArrayList<Request> Requests;
	private Catalog Catalog;
	private long id;
	private static long lastId = 0;
	public synchronized long NextId()
	{
		return ++lastId;
	}
	public TheaterClient(Catalog catalog, String fileName)
	{
		id=NextId();
		Requests=new ArrayList<Request>();
		Catalog=catalog;
		try 
		{
			FileInputStream input=new FileInputStream(fileName);
			Scanner sc=new Scanner(input);
			int lineId=0;
			while(sc.hasNext())
			{
				++lineId;
				try
				{
					Request r=GetRequestFromLine(sc.nextLine());
					Requests.add(r);
				}
				catch(TheaterException te)
				{
					System.out.println(te);
				}
				catch(Exception e)
				{
					System.out.println(e);
					System.out.println(this+"File \""+fileName+"\" line "+lineId+" is corrupted.");
				}
			}
		} 
		catch (FileNotFoundException e)
		{
			System.out.println(this+"File \""+fileName+"\" has not been found");
		}
	}
	private Request GetRequestFromLine(String line) throws Exception
	{
		Scanner sc=new Scanner(line);
		String firstName=sc.next();
		String lastName=sc.next();
		//Pattern p=Pattern.compile("\\\"(.*)\\\"");
		sc.useDelimiter("\\\"");
		sc.next();
		String eventName=sc.next();
		sc.useDelimiter("\\s");
		sc.next();
		String date=sc.next();
		String time=sc.next();
		int hallId=sc.nextInt();
		//Booking b=new Booking(firstName, lastName/*, eventName, date, time, hallId*/);
		Request r=new Request(firstName, lastName, eventName, date, time, hallId);
		while(sc.hasNext())
		{
			r.PlaceNumbers.add(new Integer(sc.nextInt()));
		}
		if(r.PlaceNumbers.isEmpty())
			throw new TheaterException(r+"Request dropped due to no seats having been requested");
		else return r;
	}
	public void run()
	{
		super.run();
		TheaterEvent e;
		for(Request r : Requests)
		{
			try
			{
				e=Catalog.GetEvent(r.EventName, r.Date, r.Time, r.HallId);
				//e.AssertPlacesAreFree(r.PlaceNumbers);
			}
			catch(TheaterException et)
			{
				
				System.out.println(et);
				System.out.println(this);
				continue;
			}
			for(Integer n : r.PlaceNumbers)
			{
				try
				{
					e.AddBooking(r.FirstName, r.LastName, n);
					System.out.println(this+"Booking Succeeded.\n");
					yield();
				}
				catch(TheaterException et)
				{
					System.out.println(this);
					System.out.println(et);
				}
			}				
		}
	}
	public String toString()
	{
		return "TheaterClient: "+id+"\n";
	}
}
